package burubaka.controller.top;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import org.slim3.controller.Navigation;
import org.slim3.util.BeanUtil;

import burubaka.BurubakaConstants;
import burubaka.bean.RankingBean;
import burubaka.common.controller.Controller;
import burubaka.model.Tipster;
import burubaka.model.TotalResult;
import burubaka.service.TipsterService;
import burubaka.service.TotalResultService;

public class RankingController extends Controller {

	private static final Logger log = Logger.getLogger(RankingController.class.getName());

	private TotalResultService totalResultService = new TotalResultService();
	private TipsterService tipsterService = new TipsterService();

	@Override
	public Navigation init() throws Exception {

		List<RankingBean> rankingList = new ArrayList<RankingBean>();
		List<TotalResult> totalList = totalResultService.getRankingForWithdrawal(BurubakaConstants.CURRENT_YEAR);

		RankingBean oldBean = new RankingBean();
		int i = 1;
		for (TotalResult totalResult : totalList) {
			RankingBean bean = new RankingBean();
			BeanUtil.copy(totalResult, bean);
			Tipster tipster = tipsterService.findByKey(totalResult.getKey().getParent());
			bean.setNickName(tipster.getNickname());

			bean.setOrder(String.valueOf(i));
			if (bean.getWithdrawal() == oldBean.getWithdrawal()) {
				bean.setOrder(oldBean.getOrder());
			}
			if (totalResult.getBets() == 0) {
				bean.setOrder("-");
			}
			rankingList.add(bean);

			oldBean = bean;
			i++;
		}

		requestScope("rankingList", rankingList);

		return forward("ranking.jsp");
	}

}
